﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string unit_Add(unit model ,int ACID);


[OperationContract]
string unit_Update(unit model ,int ACID);


[OperationContract]
string unitByunitid_Delete(int unitid,int ACID);


[OperationContract]
unit unitByunitid_Get(int unitid,int ACID );


[OperationContract]
List<unit> unit_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加unit表数据
/// <summary>
/// 添加unit
/// </summary>
/// <param name="model">unit实体</param>
/// <returns>空或错误</returns>
public string unit_Add(unit model,int ACID)
{
if(model.unitid==null )
{
return "『单位编号』不能为空！";
}
if(string.IsNullOrEmpty(model.unitname))
{
return "『单位名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.unit.Count(p=>p.unitid==model.unitid)>0)
{
return "主键已存在！";
}
if(ctx.unit.Count(p=>p.unitname==model.unitname)>0)
{
return "单位名称已存在！";
}
ctx.unit.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加unit表数据事物用重构
/// <summary>
/// 添加unit事物用重构
/// </summary>
/// <param name="model">unit实体</param>
/// <returns>空或错误</returns>
private string unit_Add(unit model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.unitid==null )
{
return "『单位编号』不能为空！";
}
if(string.IsNullOrEmpty(model.unitname))
{
return "『单位名称』不能为空！";
}
if(ctx.unit.Count(p=>p.unitid==model.unitid)>0)
{
return "主键已存在！";
}
if(ctx.unit.Count(p=>p.unitname==model.unitname)>0)
{
return "单位名称已存在！";
}
ctx.unit.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改unit表数据
/// <summary>
/// 修改unit
/// </summary>
/// <param name="model">unit实体</param>
/// <returns>空或错误</returns>
public string unit_Update(unit model,int ACID)
{
if(model.unitid==null )
{
return "『单位编号』不能为空！";
}
if(string.IsNullOrEmpty(model.unitname))
{
return "『单位名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.unit.Count(p=>p.unitid==model.unitid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
unit unitmodel=ctx.unit.Single(p => p.unitid==model.unitid);
if(model.unitid!=null )
{
unitmodel.unitid=model.unitid;
}
if(!string.IsNullOrEmpty(model.unitname))
{
unitmodel.unitname=model.unitname;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除unit表Byunitid数据
/// <summary>
/// 删除unitByunitid
/// </summary>
/// <param name="unitid">单位编号</param>
/// <returns>空或错误</returns>
public string unitByunitid_Delete(int unitid,int ACID)
{
if(unitid==null )
{
return "『单位编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.unit.Count(p=>p.unitid==unitid)!=1)
{
return null;
}
unit model=ctx.unit.Single(p => p.unitid== unitid);
ctx.unit.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除unit表Byunitid数据[事物用重构]
/// <summary>
/// 删除unitByunitid[事物用重构]
/// </summary>
/// <param name="unitid">单位编号</param>
/// <returns>空或错误</returns>
private string unitByunitid_Delete(int unitid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(unitid==null )
{
return "『单位编号』不能为空！";
}
if(ctx.unit.Count(p=>p.unitid==unitid)!=1)
{
return null;
}
unit model=ctx.unit.Single(p => p.unitid== unitid);
ctx.unit.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取unit表Byunitid数据
/// <summary>
/// 获取unitByunitid
/// </summary>
/// <param name="unitid">单位编号</param>
/// <returns>当前表</returns>
public unit unitByunitid_Get(int unitid,int ACID)
{
if(unitid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.unit.Count(p=>p.unitid==unitid)!=1)
{
return null;
}
return ctx.unit.SingleOrDefault(p=>p.unitid==unitid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取unit表数据
/// <summary>
/// 获取unit
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<unit></returns>
public List<unit> unit_Get(Expression<Func<unit, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.unit.ToList();
}
 else 
{
return ctx.unit.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.unit.Where(predicate).ToList();
}
 else 
{
return ctx.unit.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取unit表数据
/// <summary>
/// 获取unit
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<unit></returns>
public List<unit> unit_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return unit_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取unit表数据计数
/// <summary>
/// 获取unit表数据计数
/// </summary>
/// <returns>List<unit></returns>
public int unit_GetCount(Expression<Func<unit, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.unit.Count();

}
 else
{
return ctx.unit.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取unit分页表数据
/// <summary>
/// 获取unit分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<unit></returns>
public List<unit> unit_Getpage(int pageIndex, int pageSize,Expression<Func<unit, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.unit.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.unit.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.unit.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.unit.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改unit表数据
/// <summary>
/// 批量修改unit
/// </summary>
/// <param name="predicate">Expression<Func<unit, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<unit,unit>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int unit_batchUpdate(Expression<Func<unit, bool>> predicate,Expression<Func<unit, unit>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.unit.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改unit表数据事物用重构
/// <summary>
/// 批量修改unit事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<unit, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<unit,unit>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int unit_batchUpdate(Expression<Func<unit, bool>> predicate,Expression<Func<unit, unit>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.unit.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除unit表数据
/// <summary>
/// 批量删除unit
/// </summary>
/// <param name="predicate">Expression<Func<unit, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int unit_batchDel(Expression<Func<unit, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.unit.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除unit表数据事物用重构
/// <summary>
/// 批量删除unit事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<unit, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int unit_batchDel(Expression<Func<unit, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.unit.Delete(predicate);
return ok;
}
#endregion


}
}

